#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define NOISY_TEST 0

void testBuyCard(){
	int i, player;
	int seed = 1;
	int numPlayer = 2;
	//int player = 1;
	struct gameState state, state2;
	int supplyPos;
	int cardCost;
	int cur;
	int k[10] = {adventurer, council_room, feast, gardens, mine,
					remodel, smithy, village, baron, great_hall};

	int r; // = initializeGame(numPlayer, k, seed, &state);

	for(player = 0; player < numPlayer; player++){
		// Supply cards are looped from the available treasure cards.
		// Treasure cards are identified in dominion.h's CAR Denum.
		// Duc assisted with modifying function to only buy kingdom cards initialized.
	    for(i = 0; i < 10; i++){
			supplyPos = k[i]; // you buy one of 10 kingdom cards, assisted by Duc.
			memset(&state, 23, sizeof(struct gameState));
			r = initializeGame(numPlayer, k, seed, &state);
			memcpy(&state2, &state, sizeof(struct gameState));
			state.whoseTurn = player; // making sure the correct player is moving
#if (NOISY_TEST == 1)
			printf("Player: %d, Supply Card: %d, Buys Left: %d, Cards left: %d, Coins left: %d\n", player, supplyPos, state.numBuys, supplyCount(supplyPos, &state), state.coins);
#endif
			cur = buyCard(supplyPos, &state);
			cardCost = getCost(supplyPos);
#if (NOISY_TEST == 1)
			printf("Attempt Supply Card: %d, Cost: %d, Buys Left: %d, Cards left: %d, Coins left: %d\n", supplyPos, cardCost, state.numBuys, supplyCount(supplyPos, &state), state.coins);
#endif
			if(cur == -1){
				if(state.numBuys < 1){
#if (NOISY_TEST == 1)
                	printf("ERROR: No buys left.\n");
#endif
				}else if(supplyCount(supplyPos, &state) < 1){
#if (NOISY_TEST == 1)
                 	printf("ERROR: No more of %d card left.\n", supplyPos);
#endif
				}else if(state.coins < getCost(supplyPos)){
#if (NOISY_TEST == 1)
                 	printf("ERROR: You do not have enough money to buy that.\n");					
#endif
				}
			}else{
#if (NOISY_TEST == 1)
				printf("Checking coins remaining. Expected: %d. Actual %d.\n", state.coins, state2.coins - getCost(supplyPos));
#endif
				assert(state.coins == state2.coins - getCost(supplyPos));
#if (NOISY_TEST == 1)
				printf("Checking buys remaining.  Expected: %d.  Actual %d.\n", state.numBuys, state2.numBuys-1);
#endif
				assert(state.numBuys == state2.numBuys-1);
			}			
		}
	}
	printf("Finishing testBuyCard()\n");
}

int main(){
	testBuyCard();
	return 0;
}
